/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @author 品讯科技
 */

package com.px.pa.modulars.circle.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.px.basic.alone.core.base.BaseApiController;
import com.pig4cloud.pig.common.core.util.R;
import com.px.basic.alone.security.annotation.Inner;
import com.px.pa.modulars.circle.entity.CircleDynamic;
import com.px.pa.modulars.circle.entity.CircleDynamicViews;
import com.px.pa.modulars.circle.entity.CircleLearningBackage;
import com.px.pa.modulars.circle.service.CircleDynamicService;
import com.px.pa.modulars.circle.service.CircleLearningBackageService;
import com.px.pa.modulars.circle.service.CircleLearningBackageViewsService;
import com.px.pa.modulars.circle.vo.AssignedVo;
import com.px.pa.modulars.utils.TenantController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 学习包
 *
 * @author 品讯科技
 * @date 2024-08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/circlelearningbackage")
@Api(value = "circlelearningbackage", tags = "学习包管理")
public class CircleLearningBackageApiController extends TenantController {

    private final CircleLearningBackageService circleLearningBackageService;
    private final CircleLearningBackageViewsService circleLearningBackageViewsService;
    private final CircleDynamicService circleDynamicService;

    /**
     * 学习代办
     *
     * @return R
     */
    @ApiOperation(value = "学习代办", notes = "学习代办")
    @PostMapping("/learning/wait")
    @Inner(value = false)
    public R getLearningWait(){
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
      List<CircleLearningBackage> circleLearningBackageList =  circleLearningBackageService.getLearningWait(user);
      return R.ok(circleLearningBackageList);
    }


    /**
     * 学习任务进度
     *
     * @return R
     */
    @ApiOperation(value = "学习任务进度", notes = "学习任务进度")
    @PostMapping("/learning/progress")
    @Inner(value = false)
    public R getLearningProgress(){
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        Map returnMap= new HashMap();
        Map<String,Object> map =  circleLearningBackageService.getLearningProgress(returnMap,user);
        return R.ok(map);
    }

    /**
     * 学习资源列表
     *
     * @return R
     */
    @ApiOperation(value = "学习资源列表", notes = "学习资源列表")
    @GetMapping("/learning/list")
    @Inner(value = false)
    public R getMyLearningList(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize, @RequestParam(required = false) String name, @RequestParam(required = false) Integer status){
        Page page1 = new Page(page,pageSize);
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        String tenant = super.getTenant();
        Page<CircleLearningBackage> circleLearningBackagePage = circleLearningBackageService.getLearningList(page1,name,user,tenant,status);
        return R.ok(circleLearningBackagePage);
    }

    @ApiOperation(value = "学习资源列表", notes = "学习资源列表")
    @GetMapping("/learning/all_list")
    @Inner(value = false)
    public R getLearningList(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize, @RequestParam(required = false) String name){
        Page page1 = new Page(page,pageSize);
        String tenant = super.getTenant();
        Page<CircleLearningBackage> circleLearningBackagePage = circleLearningBackageService.getLearningList(page1,name,null,tenant,null);
        return R.ok(circleLearningBackagePage);
    }

    /**
     * 我未完成的学习任务
     *
     * @return R
     */
    @ApiOperation(value = "我未完成的学习任务", notes = "我未完成的学习任务")
    @GetMapping("/learning/unfinish")
    @Inner(value = false)
    public R getlearningUnfinish(@RequestParam(required = false,defaultValue = "1") Integer page,@RequestParam(required = false,defaultValue = "6") Integer pageSize){
        Page page1 = new Page(page,pageSize);
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        Map<String,Object> map =  circleLearningBackageService.getlearningUnfinish(page1,user);
        return R.ok(map);

    }

    /**
     * 我完成的学习任务
     *
     * @return R
     */
    @ApiOperation(value = "我完成的学习任务", notes = "我完成的学习任务")
    @GetMapping("/learning/finish")
    @Inner(value = false)
    public R getLearningFinish(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        Page page1 = new Page(page,pageSize);
        Map<String,Object> map =  circleLearningBackageService.getlearningfinish(page1,user);
        return R.ok(map);
    }

    /**
     * 学习包详情
     *
     * @return R
     */
    @ApiOperation(value = "学习包详情", notes = "学习包详情")
    @GetMapping("/learning/details")
    @Inner(value = false)
    public R getLearningDetails(@RequestParam("id") Integer id){
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        CircleLearningBackage circleLearningBackage =  circleLearningBackageService.getLearningDetails(id,user);
        return R.ok(circleLearningBackage);

    }


    /**
     * 观看学习包进度
     *
     * @return R
     */
    @ApiOperation(value = "观看学习包进度", notes = "观看学习包进度")
    @PostMapping("/learning/situation")
    @Inner(value = false)
    public R getlearningSituation(@RequestBody CircleDynamicViews circleDynamicViews){
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        circleDynamicViews.setUid(user);
        circleLearningBackageService.getlearningSituation(circleDynamicViews);
        return R.ok();
    }


    /**
     * 观看学习包中课程进度
     *
     * @return R
     */
    @ApiOperation(value = "观看学习包中课程进度", notes = "观看学习包中课程进度")
    @GetMapping("/learning/course/situation")
    @Inner(value = false)
    public R getLearningCourseSituation(@RequestParam("dynamic") Integer dynamic,@RequestParam("learningPageage") Integer learningPageage){
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        CircleDynamic circleDynamic =  circleLearningBackageService.getLearningCourseSituation(dynamic,learningPageage,user);
        return R.ok(circleDynamic);
    }


    /**
     * 添加学习包
     *
     * @return R
     */
    @ApiOperation(value = "添加学习包", notes = "添加学习包")
    @GetMapping("/learning/add")
    @Inner(value = false)
    public R getLearningAdd(@RequestParam("learningPageage") Integer learningPageage){
        Integer user = super.getCurrentId();

        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        circleLearningBackageService.getLearningAdd(learningPageage,user);
        return R.ok();

    }


    /**
     * 取消学习包
     *
     * @return R
     */
    @ApiOperation(value = "取消学习包", notes = "取消学习包")
    @GetMapping("/learning/cancel")
    @Inner(value = false)
    public R getLearningCancel(@RequestParam("learningPageage") Integer learningPageage){
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        circleLearningBackageService.getLearningCancel(learningPageage,user);
        return R.ok();

    }


    /**
     * 学习资源
     *
     * @return R
     */
    @ApiOperation(value = "学习资源", notes = "学习资源")
    @GetMapping("/learning/resources")
    @Inner(value = false)
    public R getLearningResources(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page page1 = new Page(page,pageSize);
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        Page<Map<String,Object>> circleLearningBackagePage = circleLearningBackageService.getListPage(page1,user);
        return R.ok(circleLearningBackagePage);
    }

    /**
     * 上次查看记录
     *
     * @return R
     */
    @ApiOperation(value = "上次查看记录", notes = "上次查看记录")
    @GetMapping("/learning/last/look")
    @Inner(value = false)
    public R getLastLook(){
        Integer user = super.getCurrentId();
        if(user == null){
            return R.failed().setMsg("请登录").setCode(401);
        }
        CircleDynamicViews circleDynamicViews = circleLearningBackageService.getLastLook(user);
        if(circleDynamicViews.getId() == null){
            return R.ok();
        }else {
            return R.ok(circleDynamicViews);
        }

    }


}
